package problems.daily;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dubulingbo, 2023/6/6 14:20.
 */
public class DT2352 {

    public int equalPairs(int[][] grid) {
        int n = grid.length;

        Map<String, Integer> rowMap = new HashMap<>();

        for (int[] g : grid) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < n; ++j) {
                sb.append(g[j]).append('#');
            }
            String k = sb.toString();
            rowMap.put(k, rowMap.getOrDefault(k, 0) + 1);
        }

        int ret = 0;
        for (int j = 0; j < n; ++j) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; ++i) {
                sb.append(grid[i][j]).append('#');
            }
            String key = sb.toString();

            int cnt = rowMap.getOrDefault(key, 0);
            if (cnt > 0) {
                ret += cnt;
            }
        }

        return ret;
    }
}
